﻿#region Usings

using System;
using Ninject;
using Rail.Net.Core.Interfaces.Data;

#endregion

namespace Rail.Net.Data
{
    public class DalContext : IDalContext
    {
        public DataContext DataContext
        {
            get
            {
                return DbContextFactory.GetThreadScopedDataContext(() => new DataContext());
            }
        }
        
        #region IDalContext Members

        public void Dispose()
        {
            if (RuRepository != null)
            {
                RuRepository.Dispose();
            }
            if (StationRepository != null)
            {
                StationRepository.Dispose();
            }
            if (RelationRepository != null)
            {
                RelationRepository.Dispose();
            }
            if (RelationRuRepository != null)
            {
                RelationRuRepository.Dispose();
            }
            if (RelationDescriptionRepository != null)
            {
                RelationDescriptionRepository.Dispose();
            }
            if (DistanceRepository != null)
            {
                DistanceRepository.Dispose();
            }
            if (PriceDistanceRepository != null)
            {
                PriceDistanceRepository.Dispose();
            }
            if (PriceSerialRepository != null)
            {
                PriceSerialRepository.Dispose();
            }
            if (PriceDomesticRepository != null)
            {
                PriceDomesticRepository.Dispose();
            }
            if (ContengentTrainLabelRepository != null)
            {
                ContengentTrainLabelRepository.Dispose();
            }
            if (ContengentTrainNumberRepository != null)
            {
                ContengentTrainNumberRepository.Dispose();
            }
            if (GlobalTrainLabelRepository != null)
            {
                GlobalTrainLabelRepository.Dispose();
            }
            if (GlobalTrainNumberRepository != null)
            {
                GlobalTrainNumberRepository.Dispose();
            }
            if (ReservableTrainLabelRepository != null)
            {
                ReservableTrainLabelRepository.Dispose();
            }
            if (ReservableTrainNumberRepository != null)
            {
                ReservableTrainNumberRepository.Dispose();
            }
            if (VirtualRuTrainNumberRepository != null)
            {
                VirtualRuTrainNumberRepository.Dispose();
            }
            if (AdministrationRepository != null)
            {
                AdministrationRepository.Dispose();
            }
            if (TariffRepository != null)
            {
                TariffRepository.Dispose();
            }
            if (PaymentOptionRepository != null)
            {
                PaymentOptionRepository.Dispose();
            }
            if (DiscountOptionRepository != null)
            {
                DiscountOptionRepository.Dispose();
            }

            try
            {
                DataContext.Dispose();
            }
            catch (Exception)
            {
                // suppress the exeption
            }

            GC.SuppressFinalize(this);
        }

        [Inject]
        public IDistanceRepository DistanceRepository { get; set; }

        [Inject]
        public IPriceDistanceRepository PriceDistanceRepository { get; set; }

        [Inject]
        public IPriceDomesticRepository PriceDomesticRepository { get; set; }

        [Inject]
        public IPriceSerialRepository PriceSerialRepository { get; set; }

        [Inject]
        public IRelationDescriptionRepository RelationDescriptionRepository { get; set; }

        [Inject]
        public IRelationRepository RelationRepository { get; set; }

        [Inject]
        public IRelationRuRepository RelationRuRepository { get; set; }

        [Inject]
        public IRuRepository RuRepository { get; set; }

        [Inject]
        public IStationRepository StationRepository { get; set; }

        [Inject]
        public IContengentTrainLabelRepository ContengentTrainLabelRepository { get; set; }

        [Inject]
        public IContengentTrainNumberRepository ContengentTrainNumberRepository { get; set; }

        [Inject]
        public IGlobalTrainLabelRepository GlobalTrainLabelRepository { get; set; }

        [Inject]
        public IGlobalTrainNumberRepository GlobalTrainNumberRepository { get; set; }

        [Inject]
        public IReservableTrainLabelRepository ReservableTrainLabelRepository { get; set; }

        [Inject]
        public IReservableTrainNumberRepository ReservableTrainNumberRepository { get; set; }

        [Inject]
        public IVirtualRuTrainNumberRepository VirtualRuTrainNumberRepository { get; set; }

        [Inject]
        public IAdministrationRepository AdministrationRepository { get; set; }

        [Inject]
        public ITariffRepository TariffRepository { get; set; }

        [Inject]
        public IPaymentOptionRepository PaymentOptionRepository { get; set; }

        [Inject]
        public IDiscountOptionRepository DiscountOptionRepository { get; set; }

        public int SaveChanges()
        {
            return DataContext.SaveChanges();
        }

        #endregion
    }
}